//
//  FeedItemsConnectionDelegate.m
//  Feed Rider
//
//  Created by Adrian-Sergiu Nicuta on 31/10/11.
//  Copyright (c) 2011 Adrisen Technologies SPRL. All rights reserved.
//

#import "FeedItemsConnectionDelegate.h"
#import "FeedParseOperation.h"

@implementation FeedItemsConnectionDelegate

@synthesize feedData;
@synthesize managedObjectContext;
@synthesize parseQueue;
@synthesize feedId;
@synthesize rootViewController;

- (id)init
{
    self = [super init];
    
    if (self) {
        parseQueue = [NSOperationQueue new];
        feedData = [[NSMutableData alloc] init];
    }
    
    return self;
}

- (void)dealloc
{
    [parseQueue release];
    [feedData release];
    
    [super dealloc];
}

- (void)handleError:(NSError *)error {
    NSString *errorMessage = [error localizedDescription];
    UIAlertView *alertView =
    [[UIAlertView alloc] initWithTitle:
     NSLocalizedString(@"Error Title",
                       @"Title for alert displayed when download or parse error occurs.")
                               message:errorMessage
                              delegate:nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

/*- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    
    if ([httpResponse statusCode] == 200) {
    } else {
    }
}*/

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    [self.feedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
    if ([error code] == kCFURLErrorNotConnectedToInternet) {
        // if we can identify the error, we can present a more precise message to the user.
        NSDictionary *userInfo =
        [NSDictionary dictionaryWithObject:
         NSLocalizedString(@"No Connection Error",
                           @"Error message displayed when not connected to the Internet.")
                                    forKey:NSLocalizedDescriptionKey];
        NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain
                                                         code:kCFURLErrorNotConnectedToInternet
                                                     userInfo:userInfo];
        [self handleError:noConnectionError];
    } else {
        // otherwise handle the error generically
        [self handleError:error];
    }
    NSString *url = connection.currentRequest.URL.absoluteString;
    [self.rootViewController.feedsConnections removeObjectForKey:url];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    FeedParseOperation *parseOperation = [[FeedParseOperation alloc] initWithData:self.feedData];
    parseOperation.currentFeedId = feedId;
    [self.parseQueue addOperation:parseOperation];
    [parseOperation release];
    
    NSString *url = connection.currentRequest.URL.absoluteString;
    [self.rootViewController.feedsConnections removeObjectForKey:url];
}


@end
